package com.eystar.ext.kit;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Model;

public class ModelKit {

    protected final static Log logger = Log.getLog(ModelKit.class);

    /**
     * Model转为Map
     * @param model
     * @return
     */
    public static Map<String, Object> toMap(Model<?> model) {
        Map<String, Object> map = new HashMap<String, Object>();
        Set<Entry<String, Object>> attrs = model._getAttrsEntrySet();
        for (Entry<String, Object> entry : attrs) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }

    /**
     * 复制一个Model
     * @param src
     * @param desc
     */
    public static void clone(Model<?> src,Model<?> desc){
        Set<Map.Entry<String, Object>> attrs =  src._getAttrsEntrySet();
        for(Map.Entry<String,Object> attr:attrs){
            String key = attr.getKey();
            Object value = attr.getValue();
            desc.set(key,value);
        }
    }
    
}
